package com.yy.controller.user;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.common.config.RCConfig;
import com.yy.common.core.AjaxResult;
import com.yy.common.core.HttpStatus;
import com.yy.common.core.model.CommunityVM;
import com.yy.common.core.page.TableDataInfo;
import com.yy.common.utils.PageHelper;
import com.yy.common.utils.file.FileUploadUtils;
import com.yy.common.utils.file.MimeTypeUtils;
import com.yy.pojo.Community;
import com.yy.pojo.User;
import com.yy.service.CommunityService;
import com.yy.service.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * ClassName: CommunityController
 * Package: com.yy.controller.user
 * Description:
 *
 * @Author yy
 * @Create 2023-12-28 19:54
 * @Version 1.0
 */
@RestController
@RequestMapping("/attachment/community")
public class CommunityController {

    @Autowired
    private UserService userService;
    @Autowired
    private CommunityService communityService;


    @PostMapping("/addPyq")
    public AjaxResult addPyq(@RequestPart(value = "commmunityPic", required = false) MultipartFile commmunityPic,
                             @RequestPart("community") CommunityVM communityVM) throws Exception {

        User user = userService.getById(communityVM.getUserId());
        Community community = new Community();
        community.setContent(communityVM.getContent());
        community.setUserId(user.getUserId());
        community.setContent(communityVM.getContent());
        community.setCreateTime(DateUtil.date());

        communityVM.setAvatar(user.getAvatar());
        communityVM.setCreateTime(DateUtil.format(community.getCreateTime(), "yyyy年MM月dd日 HH:mm:ss"));
        communityVM.setNickName(user.getNickName());

        if (ObjectUtils.isNotEmpty(commmunityPic) && !commmunityPic.isEmpty()) {
            String imageUrl = FileUploadUtils.upload(RCConfig.getAvatarPath(), commmunityPic, MimeTypeUtils.IMAGE_EXTENSION);
            community.setImageUrl(imageUrl);
            communityVM.setImageUrl(imageUrl);
        }

        boolean save = communityService.save(community);
        if (save) {
            communityVM.setCommunityId(community.getCommunityId());
            return AjaxResult.success(communityVM);
        }

        return AjaxResult.error();
    }


    @GetMapping("/receivePyq")
    public TableDataInfo receivePyq(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {

        IPage<Community> page = communityService.page(new Page<>(pageNum, pageSize), Wrappers.lambdaQuery(Community.class).orderByDesc(Community::getCreateTime));
        List<User> userList = userService.list();

        Page<CommunityVM> communityVMPage = PageHelper.copyMap(page, r -> {
            CommunityVM communityVM = new CommunityVM();
            communityVM.setContent(r.getContent());
            communityVM.setCreateTime(DateUtil.format(r.getCreateTime(), "yyyy年MM月dd日 HH:mm:ss"));
            communityVM.setImageUrl(r.getImageUrl());
            communityVM.setCommunityId(r.getCommunityId());
            userList.stream().filter(u -> r.getUserId().equals(u.getUserId())).findFirst().ifPresent(u -> {
                communityVM.setUserId(u.getUserId());
                communityVM.setAvatar(u.getAvatar());
                communityVM.setNickName(u.getNickName());
            });
            return communityVM;
        });

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(communityVMPage.getRecords());
        rspData.setTotal(communityVMPage.getTotal());
        return rspData;
    }
}
